package com.project.servlet;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

/***
 * 启动类
 */
public class MainWeb1 {
    public MainWeb1(){
        Tomcat tomcat = new Tomcat();
        //设置tomcat启动端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //创建上下文
        Context context = tomcat.addContext("",null);
        try {
            //注册servlet
         Wrapper w = Tomcat.addServlet(context,"testServlet",new TestServlet());
            //该值为正数或0，容器一启动，完成servlet的初始化动作
            //该值为负数或默认时，容器第一次访问servlet完成初始化动作
         w.setLoadOnStartup(1);
         //设置客户端请求的URL路径
         w.addMapping("/tt");

         Wrapper ws = Tomcat.addServlet(context,"json",new JsonServlet());
         ws.addMapping("/json");

            //启动Tomcat
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new MainWeb1();
    }
}
